using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace marbletrack
{
    public class Buttons
    {
        public static bool SHOWNEXTCAMVIEW = true;
        Random rand = new Random();
        public string text;
        SpriteBatch spriteBatch;
        Rectangle placement;
        Texture2D textureButton;
        public Color normalColor = Color.White;
        public Color highlightColor = Color.LightBlue;
        SpriteFont spriteFont;
        internal bool hovering;
        public bool active = true;
        
        //create a buttonClicked delegate then assign the method handler to it;
        //then call the constructor and pass your delegate in.
        //the class shoud call that if the button is clicked.
        public delegate void buttonClicked();
        public buttonClicked buttonHandler;
        public delegate void mouseOver();
        public mouseOver mouseHandler;
        const float LITTLEBUTTEXTSIZE = .85f;
        //       MouseEventHandler handler = new MouseEventHandler();
        Texture2D background = marbletrack.Game1.contentloader.Load<Texture2D>("Textures\\Woodsquare4");

        public Buttons(string Text, Rectangle Placement, buttonClicked ButtonHandler, mouseOver MouseHandler)
        {
            text = Text;
            textureButton = background;
            placement = Placement;
            buttonHandler = ButtonHandler;
            mouseHandler = MouseHandler;
            spriteFont = marbletrack.Game1.contentloader.Load<SpriteFont>("ComicSansMS");
            hovering = false;
        }

        public Buttons(Texture2D TextureForButton,Rectangle Placement, buttonClicked ButtonHandler, mouseOver MouseHandler)
        {
            text = null;
            textureButton = TextureForButton;
            placement = Placement;
            buttonHandler = ButtonHandler;
            mouseHandler = MouseHandler;
            spriteFont = marbletrack.Game1.contentloader.Load<SpriteFont>("ComicSansMS");
            hovering = false;
            spriteBatch = Game1.SpriteBatch;
        }
        public Buttons(Texture2D TextureForButton, Rectangle Placement, buttonClicked ButtonHandler)
        {
            text = null;
            textureButton = TextureForButton;
            placement = Placement;
            buttonHandler = ButtonHandler;
            spriteFont = marbletrack.Game1.contentloader.Load<SpriteFont>("ComicSansMS");
            hovering = false;
            spriteBatch = Game1.SpriteBatch;
        }

        public void Draw()
        {
            spriteBatch = marbletrack.Game1.SpriteBatch;
            spriteBatch.Begin();
            if (hovering)
            {
                spriteBatch.Draw(background, new Rectangle(placement.X + placement.Width / 20, placement.Y + placement.Height / 20, placement.Width * 9 / 10, placement.Height * 9 / 10), Color.LightBlue);
                spriteBatch.Draw(textureButton, placement, highlightColor);
            }
            else
            {
                spriteBatch.Draw(background, new Rectangle((int)(placement.X + placement.Width *(1-LITTLEBUTTEXTSIZE)/2), (int)(placement.Y + placement.Height *(1-LITTLEBUTTEXTSIZE)/2), (int)(placement.Width * LITTLEBUTTEXTSIZE),(int)( placement.Height * LITTLEBUTTEXTSIZE)), Color.White);
                spriteBatch.Draw(textureButton, placement, normalColor);
            }
            if (text != null)
            {
                spriteBatch.DrawString(spriteFont, text, new Vector2(placement.X, placement.Y), Color.BurlyWood);
            }
            spriteBatch.End();
        }

        public bool Update()
        {
            hovering = placement.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y));
            if (hovering)
            {
                if (mouseHandler != null)
                {
                    mouseHandler(); 
                }
                if (marbletrack.Game1.mouseClickedLeft)
                {
                    if (buttonHandler !=null)
                    {
                        buttonHandler(); 
                    }
                    //Game1.CheckButtons = true;
                }
            }
            return hovering;
        }

        static public void CreateStraightPiece()
        {
            if (!Game1.trackComplete)
            {
                StraightPiece piece = new StraightPiece();

                if (Game1.SendToEndOfTrack(piece) && piece.exitPoint_.Y >= 0)
                {
                    Game1.modelList.Add(piece);
                    piece.Actor.FlagDisableCollision = false; //re-enable collsions after they are disabled by the SendToEndOfTrack method
                    Game1.CheckButtons = true;
                    Game1.CheckForSupportCollisions();
                }
            }
        }

        static public void MouseOverStraightPiece()
        {
            if (!Game1.trackComplete)
            {
                Game1.selectedTrackPiece = Game1.selectedStraightPiece;
            }
        }

        static public void CreateLeftCornerPiece()
        {
            if (!Game1.trackComplete)
            {
                LeftCurvePiece piece = new LeftCurvePiece();

                if (Game1.SendToEndOfTrack(piece) && piece.exitPoint_.Y >= 0)
                {
                    Game1.modelList.Add(piece);
                    piece.Actor.FlagDisableCollision = false; //re-enable collsions after they are disabled by the SendToEndOfTrack method
                    Game1.CheckButtons = true;
                    Game1.CheckForSupportCollisions();
                }
            }
        }

        static public void MouseOverLeftCornerPiece()
        {
            if (!Game1.trackComplete)
            {
                Game1.selectedTrackPiece = Game1.selectedLeftCurvePiece;
            }
        }

        static public void CreateRightCornerPiece()
        {
            if (!Game1.trackComplete)
            {
                RightCurvePiece piece = new RightCurvePiece();

                if (Game1.SendToEndOfTrack(piece) && piece.exitPoint_.Y >= 0)
                {
                    Game1.modelList.Add(piece);
                    piece.Actor.FlagDisableCollision = false; //re-enable collsions after they are disabled by the SendToEndOfTrack method
                    Game1.CheckButtons = true;
                    Game1.CheckForSupportCollisions();
                }
            }
        }

        static public void MouseOverRightCornerPiece()
        {
            if (!Game1.trackComplete)
            {
                Game1.selectedTrackPiece = Game1.selectedRightCurvePiece;
            }
        }

        static public void CreateUpSlopePiece()
        {
            if (!Game1.trackComplete)
            {
                UpSlopePiece piece = new UpSlopePiece();
                
                if (Game1.SendToEndOfTrack(piece) && piece.exitPoint_.Y >= 0)
                {
                    Game1.modelList.Add(piece);
                    piece.Actor.FlagDisableCollision = false; //re-enable collsions after they are disabled by the SendToEndOfTrack method
                    Game1.CheckButtons = true;
                    Game1.CheckForSupportCollisions();
                }
            }
        }

        static public void MouseOverUpSlopePiece()
        {
            if (!Game1.trackComplete)
            {
                Game1.selectedTrackPiece = Game1.selectedUpSlopePiece;
            }
        }

        static public void CreateDownSlopePiece()
        {
            if (!Game1.trackComplete)
            {
                DownSlopePiece piece = new DownSlopePiece();

                if (Game1.SendToEndOfTrack(piece) && piece.exitPoint_.Y >= 0)
                {
                    Game1.modelList.Add(piece);
                    piece.Actor.FlagDisableCollision = false; //re-enable collsions after they are disabled by the SendToEndOfTrack method
                    Game1.CheckButtons = true;
                    Game1.CheckForSupportCollisions();
                }
            }
        }

        static public void MouseOverDownSlopePiece()
        {
            if (!Game1.trackComplete)
            {
                Game1.selectedTrackPiece = Game1.selectedDownSlopePiece;
            }
        }

        static public void Undo()
        {
            if (Game1.lastTrackPiece != Game1.StartingPiece)
            {
                Game1.pieceIndex--;
                Game1.RemoveTrackPiece(Game1.lastTrackPiece);
                Game1.CheckButtons = true;
                Game1.CheckForSupportCollisions();
            }
            Game1.trackComplete = false;
        }

        //static public void MoveCameraToRamp()
        //{
        //    Game1.cameraIsBall = false;
        //    Game1.cameraOnRamp = !Game1.cameraOnRamp;

        //}

        //static public void CameraIsBall()
        //{
        //    Game1.cameraIsBall = !Game1.cameraIsBall;
        //}

        //static public void RotateCameraFarRoundGrid()
        //{
        //    Game1.cameraIsBall = false;
        //    if (Game1.camposModifier != Vector3.One * 25f * Game1.SQUARESIZE)
        //    {
        //        //Game1.camposModifier = (Game1.nextCamLook - Game1.nextCamPos) / Vector3.Distance(Game1.nextCamLook, Game1.nextCamPos) * Game1.SQUARESIZE * 2f;
        //        Game1.camposModifier = Vector3.One * 25f * Game1.SQUARESIZE;
        //    }
        //    else
        //    {
        //        Game1.nextCamPos *= -1f;
        //        Game1.camposModifier *= -1f;
        //    }
        //}

        //static public void RotateCameraNearRoundGrid()
        //{
        //    Game1.cameraIsBall = false;
        //    if (Game1.camposModifier != Vector3.One * 5f * Game1.SQUARESIZE)
        //    {
        //        //Game1.camposModifier = (Game1.nextCamLook - Game1.nextCamPos) / Vector3.Distance(Game1.nextCamLook, Game1.nextCamPos) * Game1.SQUARESIZE * -1f;
        //        Game1.camposModifier = Vector3.One * 5f * Game1.SQUARESIZE;
        //    }
        //    else
        //    {
        //        Game1.nextCamPos *= -1f;
        //        Game1.camposModifier *= -1f;
        //    }
        //}

        //static public void LookLeft()
        //{
        //    Game1.camangle.Y += (float)(MathHelper.ToRadians(90));
        //    //Game1.camLookModifier = true;
        //}
        //static public void LookRight()
        //{
        //    Game1.camangle.Y += (float)(MathHelper.ToRadians(-25));
        //    Game1.camLookModifier = true;
        //}
        //static public void LookUp()
        //{
        //    Game1.camangle.X += (float)(MathHelper.ToRadians(25));
        //    Game1.camLookModifier = true;
        //}
        //static public void LookDown()
        //{
        //    Game1.camangle.X += (float)(MathHelper.ToRadians(-25));
        //    Game1.camLookModifier = true;
        //}
        //public void GoToTop()
        //{
        //    Game1.cameraIsBall = false;
        //    Game1.cameraOnRamp = true;
        //    this.buttonHandler = new buttonClicked(GoToBottem);
        //    if (SHOWNEXTCAMVIEW)
        //    {
        //        this.textureButton = Game1.contentloader.Load<Texture2D>("Textures\\Button Texture\\SmilieAtBottem");
        //    }
        //    else
        //    {
        //        this.textureButton = Game1.contentloader.Load<Texture2D>("Textures\\Button Texture\\SmilieAtTop");
        //    }
        //}

        public void GoToBottom()
        {
            Game1.cameraIsBall = true;
            this.buttonHandler = new buttonClicked(GoToBall); 
            if (SHOWNEXTCAMVIEW)
            {
                this.textureButton = Game1.contentloader.Load<Texture2D>("Textures\\Button Texture\\SmilieAtBottem");
            }
            else
            {
                this.textureButton = Game1.contentloader.Load<Texture2D>("Textures\\Button Texture\\SmilieAtBall");
            }
        }

        public void GoToBall()
        {
            Game1.cameraIsBall = false;
            this.buttonHandler = new buttonClicked(GoToBottom);
            if (SHOWNEXTCAMVIEW)
            {
                this.textureButton = Game1.contentloader.Load<Texture2D>("Textures\\Button Texture\\SmilieAtBall");
            }
            else
            {
                this.textureButton = Game1.contentloader.Load<Texture2D>("Textures\\Button Texture\\SmilieAtBottem");
            }
        }

        ///// <summary>
        ///// a random test to se if the button delgates are working. 
        ///// creates a ball near the middle of the screen.
        ///// </summary>
        //public void AddBallToList()
        //{
        //    const int ballRandomness = 50;
        //    ballList.Add(new Ball(new Vector3(random.Next(-ballRandomness, ballRandomness), random.Next(-ballRandomness, ballRandomness), random.Next(-ballRandomness, ballRandomness))));
        //}

    }
}

    
